package com.itheima.algorithm.binarysearch;

import java.util.Arrays;

public class TestBinarySearch {
    public static void main(String[] args) {
        /*
            [2,5,8]     a
            [2,0,0,0]   b
            [2,4,0,0]   b
            [2,4,5,8]   b
         */
        int[] a = {2, 5, 8};
        int target = 4;

        int i = Arrays.binarySearch(a, target);
        System.out.println(i);
        // -2 = -插入点 -1
        // -2+1 = - 插入点

        if (i < 0) {
            int index = Math.abs(i+1); // 插入点索引
            int[] b = new int[a.length+1]; // 保证插入
            System.arraycopy(a,0,b,0,index);
            b[index] = target;

            System.arraycopy(a,index,b,index+1,a.length-index);

            System.out.println(Arrays.toString(b));

        }
    }
}
